/**  
 * All rights Reserved, Designed By www.tydic.com
 * @Title:  TradesServiceImpl.java   
 * @Package com.axin.service.impl   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: axin     
 * @date:   2019年5月20日 下午11:14:30   
 * @version V1.0 
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
package com.axin.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.axin.common.entity.DataGridResult;
import com.axin.common.entity.PageObject;
import com.axin.common.utils.ListPageHelper;
import com.axin.dao.HbTradesMapper;
import com.axin.entity.HbTrades;
import com.axin.entity.example.HbTradesExample;
import com.axin.entity.example.HbTradesExample.Criteria;
import com.axin.service.MessageService;
import com.axin.service.TradesService;

/**   
 * @Description: TODO 
 * @ClassName:  TradesServiceImpl
 * @author:  Axin 
 * @date:   2019年5月20日 下午11:14:30   
 * @Copyright: 2019 www.hao456.top Inc. All rights reserved. 
 */
@Service
public class TradesServiceImpl implements TradesService {

	@Autowired
	private HbTradesMapper tradesMapper;
	
	@Autowired
	private MessageService messageService;
	/* (non-Javadoc)
	 * @see com.axin.service.TradesService#saveTrades(com.axin.entity.HbTrades, java.lang.Long)
	 */
	@Override
	public void saveTrades(HbTrades trades, Long userId) {
		
		trades.setUserId(userId);
		trades.setCreateTime(new Date());
		this.tradesMapper.insert(trades);
		try {
			if("1".equals(trades.getTradeDirect())){
				messageService.sendBuyTradesUpdateNotify(userId);
			}else if("2".equals(trades.getTradeDirect())){
				messageService.sendSellTradesUpdateNotify(userId);
			}
		} catch (Exception e) {
			
		}
		//
	}

	/* (non-Javadoc)
	 * @see com.axin.service.TradesService#getListByParam(com.axin.common.entity.PageObject, com.axin.entity.HbTrades)
	 */
	@Override
	public DataGridResult getListByParam(PageObject page, HbTrades trades,Long userId) {
		HbTradesExample example = new HbTradesExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		if(StringUtils.isNotEmpty(trades.getTradeDirect())){
			criteria.andTradeDirectEqualTo(trades.getTradeDirect());
		}
		if(StringUtils.isNotEmpty(trades.getSymbol())){
			criteria.andSymbolEqualTo(trades.getSymbol());
		}
		if(trades.getTaskId() != null){
			criteria.andTaskIdEqualTo(trades.getTaskId());
		}
		example.setOrderByClause("trade_time DESC");
		List<HbTrades> list = this.tradesMapper.selectByExample(example);
		
		int size = list.size();
		 if(page != null){
			 ListPageHelper<HbTrades> pageHelper = new ListPageHelper<>();
			 try {
				list = pageHelper.page(page.getPage(), page.getRows(), list);
			} catch (Exception e) {
				e.printStackTrace();
			}
		 }
		 
		 return new DataGridResult(size,list);
	}

	/* (non-Javadoc)
	 * @see com.axin.service.TradesService#getGroupListByParam(com.axin.common.entity.PageObject, com.axin.entity.HbTrades, java.lang.Long)
	 */
	@Override
	public DataGridResult getGroupListByParam(PageObject page, HbTrades trades,String sort, String order, Long userId) {
		HbTradesExample example = new HbTradesExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserIdEqualTo(userId);
		
		if("tradeTime".equals(sort)){
			sort = "trade_time";
		}
		example.setOrderByClause(sort + " " + order);
		
		List<HbTrades> list = this.tradesMapper.selectByExample(example);
		
		int size = list.size();
		 ListPageHelper<HbTrades> pageHelper = new ListPageHelper<>();
		 try {
			list = pageHelper.page(page.getPage(), page.getRows(), list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for (HbTrades item : list) {
			Long taskId = item.getTaskId();
			String symbol = item.getSymbol();
			Integer loopCount = item.getLoopCount();
			String groupId = taskId + "-" + symbol + "-" + loopCount;
			item.setGroupId(groupId);
		}
		
		return new DataGridResult(size,list);
	}
	
	
	


}
